//
//  StarLabel.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/12/4.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "StarLabel.h"
#import "UIView+Sizes.h"

#define kStarImageViewRightMargin 5

@implementation StarLabel

- (instancetype)init{
    self = [super init];
    if (self) {
        self.textAlignment = NSTextAlignmentLeft;
        self.numberOfLines = 1;
        
        _starButton = [[UIButton alloc] init];
        [self addSubview:_starButton];
        
        self.userInteractionEnabled = YES;
        
        UIImage *unselectedImage = [UIImage imageNamed:@"home_start_nosele.png"];
        UIImage *selectedImage = [UIImage imageNamed:@"home_start_issele.png"];
        
        [_starButton addTarget:self.superview action:@selector(selectChanged) forControlEvents:UIControlEventTouchUpInside];
        
        [_starButton setImage:selectedImage forState:UIControlStateSelected];
        [_starButton setImage:unselectedImage forState:UIControlStateNormal];
    }
    return self;
}

- (void)layoutSubviews{
    CGFloat width = self.height - 2 * kStarImageViewRightMargin;
    _starButton.frame = CGRectMake(self.frame.size.width - kStarImageViewRightMargin - width, kStarImageViewRightMargin, width, width);
    _starButton.selected = _selected;
}

- (void)selectChanged{
    _selected = !_selected;
    [self setNeedsLayout];
}

@end
